package com.tfswx.ruleengine.database.bean;

import com.tfswx.ruleengine.database.enums.SjklxEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Ricky
 * @date 2021/7/10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TabeleField {
    private String zdmc;
    private Zdlx zdlx;
    private Integer zddx;
    private Boolean zj;
    private String zs;

    /**
     * mysql字段类型（长度）
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getMysqlZdlxStr() {
        String name = zdlx.getMYSQL();
        String zdlxStr = zddx == null ? name : name + "(" + zddx + ")";

        StringBuilder sb = new StringBuilder().append(zdmc).append(" ").append(zdlxStr);
        if (Boolean.TRUE.equals(zj)) {
            sb.append(" ").append("NOT NULL PRIMARY KEY");
        } else {
            sb.append(" ").append("NULL");
        }
        sb.append(" ").append("COMMENT '").append(zs).append("'");
        return sb.toString();
    }


    /**
     * 达梦字段类型（长度）
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getDmZdlxStr() {
        String name = zdlx.getDM();
        String zdlxStr = zddx == null ? name : name + "(" + zddx + ")";

        StringBuilder sb = new StringBuilder().append(zdmc).append(" ").append(zdlxStr);
        if (Boolean.TRUE.equals(zj)) {
            sb.append(" ").append("NOT NULL");
        } else {
            sb.append(" ").append("NULL");
        }
        return sb.toString();
    }

    private String getSqliteZdlxStr() {
        String name = zdlx.getSQLITE();
        String zdlxStr = zddx == null ? name : name + "(" + zddx + ")";

        StringBuilder sb = new StringBuilder().append(zdmc).append(" ").append(zdlxStr);
        if (Boolean.TRUE.equals(zj)) {
            sb.append(" ").append("NOT NULL");
        } else {
            sb.append(" ").append("NULL");
        }
        return sb.toString();
    }

    /**
     * 达梦创建字段注释
     *
     * @param tableName tableName
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getDmCommentSql(String tableName) {
        return "COMMENT ON COLUMN " + tableName + "." + zdmc + " IS '" + zs + "';";
    }

    public String getZdlxStr(SjklxEnum sjklxEnum) {
        switch (sjklxEnum) {
            case DM:
                return getDmZdlxStr();
            case MYSQL:
                return getMysqlZdlxStr();
            case SQLITE:
                return getSqliteZdlxStr();
            default:
        }
        throw new RuntimeException("数据库不支持");
    }
}
